# THIS SCRIPT COMPUTES SUMMARY STATISTICS FOR FIGURE 14, AND PROVIDES
# THE SYNTAX FOR CREATING THE FIGURE ITSELF. IT MUST BE RUN IN CONJUNCTION 
# WITH THE DATASET "ANESCumulative_cleaned.csv", AVAILABLE IN THE REPLICATION 
# MATERIALS ON DATAVERSE. 
rm(list = ls())
library(foreign)
library(car)
library(readstata13)
library(ggplot2)
library(stargazer)
library(grid)
library(gridExtra)
library(survey)
library(dplyr)
library(lemon)
library(ggpubr)
library(reshape2)

# READ IN THE DATA. SELECT THE FILE "ANESCumulative_cleaned.csv"
our.data = read.csv(file.choose())
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# DATAFRAME: REPUBLICANS
attach(our.data)
gop.data = data.frame(matchedGOP_abortion, matchedGOP_guncontrol, 
                      matchedGOP_MTtraditionalvalues, matchedGOP_MTadjustviews,
                      matchedGOP_MTtoleratenew, matchedGOP_MTnewlifestyles, 
                      year, VCF0009z, parties3)
gop.data = subset(gop.data, parties3=="Republican")
detach(our.data)

# DATAFRAME: DEMOCRATS
attach(our.data)
dems.data = data.frame(matchedDEM_abortion, matchedDEM_guncontrol, 
                       matchedDEM_MTtraditionalvalues, matchedDEM_MTadjustviews,
                       matchedDEM_MTtoleratenew, matchedDEM_MTnewlifestyles, 
                       year, VCF0009z, parties3)
dems.data = subset(dems.data, parties3=="Democrat")
detach(our.data)
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# DESIGNS
gop.design = svydesign(ids=~1, weight=~gop.data$VCF0009z,
                       data = gop.data)
dems.design = svydesign(ids=~1, weight=~dems.data$VCF0009z,
                        data = dems.data)

# FOR LOOP
our.means.GOP = list(list())
our.props.GOP = list(list())
our.means.DEM = list(list())
our.props.DEM = list(list())
for(i in 1:6){
  # REPUBLICANS
  our.means.GOP[[i]] = svytable(~ gop.data[, i] + year, 
                                design = gop.design)
  our.props.GOP[[i]] = prop.table(our.means.GOP[[i]], 2)
  our.props.GOP[[i]]
  our.props.GOP[[i]] = as.data.frame(our.props.GOP[[i]])
  our.props.GOP[[i]] = subset(our.props.GOP[[i]], our.props.GOP[[i]][, 1]==1 & Freq!=0)
  names(our.props.GOP[[i]])[1] = "outcome"
  # DEMOCRATS 
  our.means.DEM[[i]] = svytable(~ dems.data[, i] + year, 
                                design = dems.design)
  our.props.DEM[[i]] = prop.table(our.means.DEM[[i]], 2)
  our.props.DEM[[i]]
  our.props.DEM[[i]] = as.data.frame(our.props.DEM[[i]])
  our.props.DEM[[i]] = subset(our.props.DEM[[i]], our.props.DEM[[i]][, 1]==1 & Freq!=0)
  names(our.props.DEM[[i]])[1] = "outcome"
}

# PLOTTING DATA
newer.data = rbind(our.props.GOP[[1]], 
                   our.props.GOP[[2]], 
                   our.props.GOP[[3]],
                   our.props.GOP[[4]], 
                   our.props.GOP[[5]], 
                   our.props.GOP[[6]],
                   our.props.DEM[[1]], 
                   our.props.DEM[[2]], 
                   our.props.DEM[[3]],
                   our.props.DEM[[4]], 
                   our.props.DEM[[5]], 
                   our.props.DEM[[6]])
head(newer.data)
newer.data = newer.data[-c(1)]
newer.data$parties = c(rep("Republicans", 72),
                       rep("Democrats", 72))
newer.data$issue = rep(c(rep("Abortion", length(our.props.GOP[[1]]$Freq)),
                         rep("Gun control", length(our.props.GOP[[2]]$Freq)),
                         rep("Moral trad.: respect \ntraditional family ties", length(our.props.GOP[[3]]$Freq)),
                         rep("Moral trad.: adjust views \nbecause world changing", length(our.props.GOP[[4]]$Freq)),
                         rep("Moral trad.: tolerate \ndifferent moral standards", length(our.props.GOP[[5]]$Freq)),
                         rep("Moral trad.: new \nlifestyles causing breakdown", length(our.props.GOP[[6]]$Freq))), 2)
newer.data$year = as.numeric(as.character(newer.data$year))
newer.data
######################################################################################
######################################################################################
######################################################################################
######################################################################################
######################################################################################
# FIGURE 14: PARTY COHESION ON CULTURAL ATTITUDES: MATCHED AND ISSUE-SORTED
# PARTISANS IN THE PARTY COALITIONS, 1980-2020. 
our.plot = ggplot(newer.data,
                  aes(year, Freq, group = parties, 
                      linetype =  parties)) +
  geom_line(aes(linetype = parties), linewidth = 1) +
  labs(linetype = "Party ID") +  
  xlab("") +
  ylab("Proportion\n") + 
  theme_minimal() +
  # ggtitle("Party Cohesion on Cultural Attitudes: Matched and Issue-Sorted \nPartisans in the Party Coalitions, 1980-2020", 
  #        subtitle = "Data: ANES cumulative file. Estimates indicate the proportion of Republican or \nDemocratic identifiers that matched their party's ideological orientation \nand shared their party's position.") + 
  # scale_color_grey(start = 0.8, end = 0.2) +
  facet_wrap(~issue, ncol = 2, scales = "free_x") +
  # theme(legend.title = element_text(face = "bold")) +
  theme(legend.position = "none") +
  # theme(legend.title = element_text(size = 12)) +
  # theme(legend.text = element_text(size = 12)) +
  theme(axis.title.x = element_text(size = 9)) + 
  theme(axis.title.y = element_text(size = 9)) +
  # theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 14)) + 
  # theme(plot.subtitle = element_text(hjust = 0.5, size = 12)) +
  theme(strip.text = element_text(face = "bold", size = 9)) +
  theme(panel.spacing = unit(1, "lines"))
our.plot

# SAVE
# ggsave(our.plot, file = "Schmidtetal-Figure14.pdf", width = 6.6, height = 9)
